<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller{

    /**
     * index  
     * 
     * @param Request $request 
     * @access public
     * @return void
     */
    public function index(Request $request){
        $where[] = ['status','=',1];
        $articles =Article::where($where)->orderBy('sort','desc')->orderBy('id','desc')->paginate(5);
        return view('home.news.index',compact('articles'));
    }

    /**
     * [rules 交易规则]
     * @param  Request $request [description]
     * @param  [type]  $symbol  [description]
     * @return [type]           [description]
     */
    public function detail(Request $request,$id){
         $article = Article::find($id);
         return view('home.news.detail',compact('article'));
    }

    public function redict($maxId=0)
    {
        $url="http://m.jin10.com/flash?maxId=0";
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5);

        curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5' );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        // 执行一个curl会话
        $data = curl_exec($ch);
        $data = json_decode($data);

        foreach($data as $k=>$v){
            $d=preg_replace('/金十/ism','',$v);
            $d=preg_replace('/<a href=".*?".*?>/ism','<a href="javascript:void(0)">',$d);
            //$v=preg_replace('/<\\/a>/ism','',$v);
            $data[$k] = $d;
        }
        $data = json_encode($data);
        
        return $data;
    }

    public function ajax_add()
    {
        $url="http://m.jin10.com/index";
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5);

        curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5' );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        // 执行一个curl会话
        $data = curl_exec($ch);
        //var_dump($data);
        $data=preg_replace('/<a href=".*?".*?>/ism','<a href="javascript:void(0)">',$data);
        //$data=preg_replace('/金十/ism','',$data);
        return response()->json(['data'=>$data]);
    }

}


	
